import api from "@/store/api/index.js";
import { createSlice } from "@reduxjs/toolkit";
export const baseSlice = createSlice({
  name: "base", //标志action中type的前缀
  initialState: {
    userInfo: null,
  },
  reducers: {
    //操作state的各种方法
    setUserInfo(state, action) {
      state.userInfo = action.payload;
    },
    clearUserInfo(state) {
      state.userInfo = null;
    },
  },
});
export const { setUserInfo, clearUserInfo } = baseSlice.actions;
export const setUserInfoAsync = () => {
  return async (dispatch) => {
    let info;
    try {
      let { code, data } = await api.queryUserInfo();
      if (+code === 0) {
        info = data;
      }
    } catch (_) {}
    dispatch(setUserInfo(info));
  };
};
